
$(document).ready(function() {
	// 初始化表格
	initTable();
	initCitys();
	$("#informationButton_win").window('close');
	$("#informationText").val('');
	$("#cityId").bind('change' , function(){
		var cityId = $("#cityId").val();
		if(cityId != -1){
			var param = {"orgId":cityId,"isOrg":"1"};
			$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
				if(data!=null && data.success == true){
					var obj = data.data.resultData;
					var ops = '<option value="-1">-----请选择-----</option>';
					$.each(obj  , function(index , org){
						ops += '<option value="'+org.id+'">'+org.orgName+'</option>';
					});
					$("#countyId").html(ops);
					$("#schoolId").html('<option value="-1">-----请选择-----</option>');
					$("#gradeId").html('<option value="-1">-----请选择-----</option>');
				}
			})
		}else{
			$("#countyId").html('<option value="-1">-----请选择-----</option>');
			$("#schoolId").html('<option value="-1">-----请选择-----</option>');
			$("#gradeId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	
	$("#countyId").bind('change' , function(){
		var countyId = $("#countyId").val();
		if(countyId != -1){
			var param = {"orgId":countyId,"isOrg":"2"};
			$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
				if(data!=null && data.success == true){
					var obj = data.data.resultData;
					var ops = '<option value="-1">-----请选择-----</option>';
					$.each(obj  , function(index , school){
						ops += '<option value="'+school.id+'" op_phaseType="'+school.phaseType+'">'+school.schoolName+'</option>';
					});
					$("#schoolId").html(ops);
					
					$("#gradeId").html('<option value="-1">-----请选择-----</option>');
				}
			});
		}else{
			$("#schoolId").html('<option value="-1">-----请选择-----</option>');
			$("#gradeId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	
	$("#schoolId").bind('change' , function(){ // 获取当前学校的年级  班级
		var schoolId = $("#schoolId").val();
		var phaseType=$("#schoolId").find("option:selected").attr("op_phaseType");
		if(schoolId != -1){
			var param = {"orgId":schoolId+"_"+phaseType,"isOrg":"3"};
			$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
				if(data!=null && data.success == true){
					var obj = data.data.resultData;
					obj.sort(function(a,b){return Number(b.gradeId) - Number(a.gradeId);});
					var ops = '<option value="-1">-----请选择-----</option>';
					$.each(obj  , function(index , grade){
						ops += '<option value="'+grade.gradeId+'" >'+grade.gradeName+'</option>';
					});
					$("#gradeId").html(ops);
				}
			});
		}else{
			$("#gradeId").html('<option value="-1">-----请选择-----</option>');
		}
	});
	
	$("#gradeId").bind('change' , function(){
		var gradeId = $("#gradeId").val();
	});
	
	$("#roleCode").bind('change' , function(){
		var roleCode = $("#roleCode").val();
		if(roleCode == 3){//教育工作者
			$("#roleCode1").show();
		}else{
			$("#roleCode1").hide();
		}
	});
	$("#roleCode1").bind('change' , function(){
	});
	
	// 查询按钮
	$("#searchBtn").click(function(){
		doSearch();
	});
	
	function doSearch(){
		var rn = Math.random();
		$('#tt').datagrid( 'getPager').pagination({
	            'pageNumber' : 1
	    });
		$('#tt').datagrid( 'options').url=mainDomain +'/user/queryInformationTouchUsersByDto.action?rn=' +rn;
		$('#tt').datagrid( 'options').queryParams = getParams();
		$('#tt').datagrid('reload');
	}
	
	//重置按钮
	$("#clearForm").click(function(){
		$("#provinceId").val(-1);
		$("#cityId").val(-1);
		$("#countyId").val(-1);
		$("#schoolId").val(-1);
		$("#gradeId").val(-1);
		$("#roleCode").val(-1);
		$("#deviceType").val(-1);
		$("#confirmFlag").val(-1);
	});
	
	$("#informationButton").click(function(){
		backendCommon.openeWin('informationButton_win');
		var obj = $('#tt').datagrid('getSelections');
		if(obj.length == 0){
			$('#tt').datagrid('selectAll');
		}
		var provinceName = $("#provinceId").find("option:selected").text()+"-";
		var cityId = $("#cityId").val();
		var countyId = $("#countyId").val();
		var schoolId = $("#schoolId").val();
		var gradeId = $("#gradeId").val();
		if(cityId != -1){
			provinceName += $("#cityId").find("option:selected").text()+"-";
		}
		if(countyId != -1 ){
			provinceName += $("#countyId").find("option:selected").text()+"-";
		}
		if( schoolId!=-1 ){
			var phaseType=$("#schoolId").find("option:selected").attr("op_phaseType");
			if(phaseType == 1){
				provinceName += $("#schoolId").find("option:selected").text()+"(小学)-";
			}else if(phaseType == 2){
				provinceName += $("#schoolId").find("option:selected").text()+"(初中)-";
			}else if(phaseType == 3){
				provinceName += $("#schoolId").find("option:selected").text()+"(高中)-";
			}else{
				provinceName += $("#schoolId").find("option:selected").text()+"-";
			}
		}
		if(  gradeId != -1){
			provinceName += $("#gradeId").find("option:selected").text()+"-";
		}
		var grid = $('#tt');
		var options = grid.datagrid('getPager').data("pagination").options;
		$("#org_info").html('<span style="color:red;font-size:10px;">'+provinceName.substring(0,provinceName.length-1) +'</span>');
		
		var curr = options.pageNumber;
		$("#page_info").html('<span style="color:red;font-size:10px;">第'+curr +'页</span>');
	});
	
	
	function getParams(){
		var provinceId = $("#provinceId").val();
		var cityId = $("#cityId").val();
		var countyId = $("#countyId").val();
		var schoolId = $("#schoolId").val();
		var gradeId = $("#gradeId").val();
		var roleCode = $("#roleCode").val();
		var roleCode1 = $("#roleCode1").val();
		// 推送设备类型
		var deviceType = $("#deviceType").val();
		// 是否学籍验证
		var confirmFlag = $("#confirmFlag").val();
		if(confirmFlag == -1){
			confirmFlag = '';
		}
		//判断是否是全省       provinceId 默认为1  和role 
		if(roleCode == -1){
			roleCode = '';
		}
		if(roleCode == 3 && roleCode1!= -1){
			roleCode = roleCode1;
		}
		if(cityId == -1 ){
			cityId = '';
		}
		if(countyId == -1 ){
			countyId = '';
		}
		if( schoolId==-1 ){
			schoolId = '';
		}
		if(  gradeId == -1){
			gradeId = '';
		}
		var params = {
				"roleCode":roleCode,
				"provinceOrgId":provinceId,
				"cityOrgId":cityId,
				"countyOrgId":countyId,
				"schoolId":schoolId,
				"gradeId":gradeId,
				"deviceType":deviceType,
				"confirmFlag":confirmFlag,
				 rn : Math.random()
		};
		return params;
	}
	
	//发送确认
	$("#toInformation").click(function(){
		var checkflag = $("input[name='checkUser']:checked").val();
		var informationText = $("#informationText").val().replace(/(^\s*)|(\s*$)/g, "");
		if(informationText==""){
			$.messager.alert('失败','请输入资讯信息');
			return false;
		}
		var userIds = [];
		var informationType = 1;
		var params ={};
		if(checkflag == 1){//全组织
			params = getParams();
			informationType = 1;
		}else if(checkflag == 2){
			var rows = $('#tt').datagrid('getSelections');
			for(var i=0; i<rows.length; i++){
				userIds.push(rows[i].userId);
			}
			params['userIds']=userIds.toString();
			informationType = 2;
		}
		params["informationType"]=informationType;
		params["informationText"]=informationText;
		$.post(mainDomain + '/user/sendInformationToUser.action' , params , function(data){
			if(data.success){
				$.messager.alert('消息','推送成功！');
			}else{
				$.messager.alert('失败',data.message);
			}
			$("#informationButton_win").window('close');
			//$('#tt').datagrid("clearSelections");
		});
	});
	
	$("#closeWin").click(function(){
		backendCommon.closeWin('informationButton_win');
		//$('#tt').datagrid("clearSelections");
	});
	
//	$("#informationButton_win").window({
//		onClose:function(){
//			$('#tt').datagrid("clearSelections");
//		}
//	});
});

function initCitys(){
	//清除市  区  学校    年级   班级 
	var provinceId = $("#provinceId").val();
	var param = {"orgId":provinceId,"isOrg":"1"};
	$.post(mainDomain + '/user/findChildrenOrgByParentOrgId.action' , param , function(data){
		if(data!=null && data.success == true){
			var obj = data.data.resultData;
			var ops = '<option value="-1">-----请选择-----</option>';
			$.each(obj  , function(index , org){
				ops += '<option value="'+org.id+'">'+org.orgName+'</option>';
			});
			$("#cityId").html(ops);
		}
	});
}

// 初始化table
function initTable() {
	var roleCode = $("#roleCode").val();
	var roleCode1 = $("#roleCode1").val();
	//判断是否是全省       provinceId 默认为1  和role 
	if(roleCode == -1){
		roleCode = '';
	}
	if(roleCode == 3 && roleCode1!= -1){
		roleCode = roleCode1;
	}
	$('#tt').datagrid({
		width : backendCommon.getWidth(0.99),
//		url : mainDomain+'/user/queryInformationTouchUsersByDto.action',
		nowrap : false,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect: false,
		selectOnCheck: true,
		checkOnSelect: true,
		pageSize :20,
		pageList: [20,50,100],
		queryParams : {
			'provinceOrgId':1,
			'roleCode':roleCode,
			"rn" : Math.random()
		},
		columns : [ [
		        { field:'ck',checkbox:true },
				{
					field : 'userId',
					title : '用户ID',
					width:60,
					align : 'center'
				},
				{
					field : 'userName',
					title : '用户名',
					width:120,
					align : 'center'
				},
				{
					field : 'roleCode',
					title : '用户角色',
					width:80,
					align : 'center',
					formatter:function(value,rec){
						if(value == 1){
							return '学生';
						}else if(value == 11){
							return '省管理员';
						}else if(value == 12){
							return '市管理员';
						}else if(value == 13){
							return '区管理员';
						}else if(value == 14){
							return '管理员';
						}else if(value == 15){
							return '市处室';
						}else if(value == 16){
							return '区处室';
						}else if(value == 2){
							return '学校管理员';
						}else if(value == 3){
							return '年级管理员';
						}else if(value == 4){
							return '班主任';
						}else if(value == 5){
							return '老师';
						}else if(value == 17){
							return '校处室';
						}else if(value == 33){
							return '机构人员';
						}else if(value == 6){
							return '老师兼机构人员';
						}else if(value == 7){
							return '班主任兼机构人员';
						}
					}
				},
				{
					field : 'schoolName',
					title : '学校',
					width:200,
					align : 'center'
				},
				{
					field : 'confirmFlag',
					title : '学籍是否确认',
					width:120,
					align : 'center',
					formatter:function(value,rec){
						if(value == 1){
							return "<font color='green'>已确认</font>";
						}else if(value == 0){
							return "<font color='red'>未确认</font>";
						}else{
							return "--";
						}
					}
				},
				{
					field : 'deviceType',
					title : '设备类型',
					width:120,
					align : 'center',
					formatter:function(value,rec){
						var str = "--";
						if(value == 0){
							str = "pc";
						}else if(value == 1){
							str = "android";
						}else if(value == 2){
							str = "ios";
						}
						return str;
					}
				},
		] ]
	});
}

